<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <TreeTools
          :tree-data="company"
          @openDialog="openDialog"
          :is-root="true"
        />
        <el-tree :data="data" :props="{ label: 'name', children: 'children' }">
          <TreeTools
            slot-scope="{ data }"
            :tree-data="data"
            @updateList="initData"
            @openDialog="openDialog"
            @editDialog="editDialog"
        /></el-tree>
      </el-card>
    </div>
    <add-dept ref="addDept" />
  </div>
</template>

<script>
import TreeTools from './components/tree-tools.vue'
import { getDepartmentListApi } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index'
import addDept from './components/add-dept.vue'
export default {
  name: 'Departments',
  data() {
    return {
      company: { name: '传智教育', manager: ' 负责人', id: '' },
      data: [
        {
          name: '总裁办',
          manager: '曹操',
          child: [{ name: '董事会', manager: '曹丕' }]
        },
        {
          name: '行政部',
          manager: '刘备'
        },
        {
          name: '人事部',
          manager: '孙权'
        }
      ]
    }
  },
  components: {
    TreeTools,
    addDept
  },
  created() {
    this.initData()
  },
  methods: {
    async initData() {
      const { companyName, depts } = await getDepartmentListApi()
      this.company.name = companyName
      this.data = tranListToTreeData(depts, '')
    },
    openDialog(val) {
      this.$refs.addDept.dialogShow = true
      // console.log(this.$refs.addDept)
      this.$refs.addDept.formData.pid = val
      // console.log(val)
    },
    editDialog(val) {
      this.$refs.addDept.getFormDataInfo(val)
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
